#!/usr/bin/env python3

from bs4 import BeautifulSoup
import os
import sys
from os.path import abspath, dirname

info = []
html = "%s%s%s%s%s" \
    % (dirname(abspath(sys.argv[0])), os.sep, "aa", os.sep, "index.html")
print(html)

with open(html) as haha:
    soup = BeautifulSoup(haha, 'lxml')
    titles = soup.select("body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a")
    pics = soup.select("body > div > div > div.col-md-9 > div > div > div > img")
    prices = soup.select("body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right")
    stars = soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)") #取父级标签的第二个p，即所有星星。
    reviews = soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right")
    # print(type(stars))

    # print(titles, pics, prices, stars, reviews, sep = "\n=========\n")

for (title, pic, price, star, review) in zip(titles, pics, prices, stars, reviews):
    # print(type(star))
    data = {
        'title': title.get_text(),
        'pic': pic['src'],
        'price': price.get_text(),
        'star': len(star.find_all("span","glyphicon glyphicon-star")), #使用find_all()来找出所有星星，然后计算返回的列表长度即可
        'review': review.get_text()
    }
    info.append(data)
print(info)


'''
titles; body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4:nth-child(2) > a
pics; body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > img
prices; body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4.pull-right
stars; body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p:nth-child(2) > span:nth-child(1)
reviews; body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p.pull-right
'''
